
module.exports = {
    //时间格式化，输出格式 YYYY-MM-DD HH:MM:SS
    //time: now、+|- num  Y|M|D|h|m|s   示例：+180D、或其他可被new Date()识别的时间格式
    format(time, isPadding = true, dateChar = "-", timeChar = ":") {
        if (/^now$/.test(time)) {
            time = new Date();
        } else if (/^[\+-]\d+[YMDhms]$/.test(time)) {
            time=this.byTimeLong("now",time)
        }
        let timeStr = new Date(time).toLocaleString('chinese', { hour12: false }).replace(/\//g, dateChar);
        timeChar != ":" && (timeStr = timeStr.replace(/:/g, timeChar));
        if (isPadding) {
            timeStr = timeStr.split(" ").map((item, index) => {
                let splitChar = index == 0 ? dateChar : timeChar;
                item = item.split(splitChar).map(ite => {
                    ite = ite.padStart(2, "0");
                    return ite;
                }).join(splitChar);
                return item;
            }).join(" ");
        }
        return timeStr;
    },
    //获取现在的时间
    now() {
        let nowTime = new Date().toLocaleString('chinese', { hour12: false });
        return this.format(nowTime);
    },
    //根据一个时间跨度获取指定的时间
    //time: now或其他可被new Date()识别的时间格式
    // long: +|- num  Y|M|D|h|m|s   示例：+180D
    //注意：涉及到具体月份、年份相加减的，这里统一按1年=365天，1月=30天计算
    byTimeLong(time, long) {
        // let timeMs = new Date(this.format(time, false)).getTime();
        // let longMs = new Date(this.format(long, false)).getTime();
        // let longReg = /^[\+-][0-9]+[YMDhms]$/;
        // if (!longReg.test(long)) {
        //     throw new Error("long参数格式不对");
        // }
        // time == "now" && (time = new Date().toLocaleString('chinese', { hour12: false }));
        let unit = long.charAt(long.length - 1);
        let num = parseInt(long);
        let timeMs = new Date(this.format(time, false)).getTime(), longMs = 0;
        switch (unit) {
            case "Y": longMs = num * 3600000 * 24 * 365; break;
            case "M": longMs = num * 3600000 * 24 * 30; break;
            case "D": longMs = num * 3600000 * 24; break;
            case "h": longMs = num * 3600000; break;
            case "m": longMs = num * 1000 * 60; break;
            case "s": longMs = num * 1000; break;
            default: break;
        }
        let endMs = timeMs + longMs;
        return this.format(new Date(endMs));
    },
    //str格式: now、  +|- 180 Y|M|D|h|m|s、  YYYY-MM-DD HH:MM:SS  、YYYY-M-D H:M:S
    // format(str) {
    //     let time = "";
    //     // let strReg=/^now$|^[\+-]\d+[YMDhms]$|^\d{4}(-\d{1,2}){2} \d{1,2}(:\d{1,2}){2}$/;
    //     if (/^\d{4}(-\d{1,2}){2} \d{1,2}(:\d{1,2}){2}$/.test(str)) {
    //         time = str;
    //     } else if (/^now$/.test(str)) {
    //         time = this.now()
    //     } else if (/^[\+-]\d+[YMDhms]$/.test(str)) {
    //         time = this.byTimeLong("now", str);
    //     } else {
    //         throw new Error("参数有误")
    //     }
    //     return time;
    // },
    // 计算时间差值
    //unit: 时间差的单位 Y,M,D,h,m,s,ms   返回数值，不含单位
    diff(time_1 = "now", time_2 = "now", unit = "D") {
        let strReg = /^now$|^[\+-]\d+[YMDhms]$/;
        strReg.test(time_1) && (time_1 = this.format(time_1));
        strReg.test(time_2) && (time_2 = this.format(time_2));
        let ms_1 = new Date(time_1).getTime();
        let ms_2 = new Date(time_2).getTime();
        let diffMs = Math.abs(ms_1 - ms_2);
        let end = "";
        switch (unit) {
            case "Y": end += diffMs / (1000 * 3600 * 24 * 360); break;
            case "M": end += diffMs / (1000 * 3600 * 24 * 30); break;
            case "D": end += diffMs / (1000 * 3600 * 24); break;
            case "h": end += diffMs / (1000 * 3600); break;
            case "m": end += diffMs / (1000 * 60); break;
            case "s": end += diffMs / (1000); break;
            case "ms": end += diffMs; break;
            default: break;
        }
        return end;
    },
    // compare(time_1,time_2){
    //     let endStr="=";
    //     time_1=this.format(time_1);
    //     time_2=this.format(time_2);
    //     if(time_1>time_2){
    //         endStr=">";
    //     }else if(time_1<time_2){
    //         endStr="<";
    //     }
    //     return endStr;
    // }
}